<template>
  <div class="aui-card-list">
  <uni-list-query-card :queryShow="queryShow" @query-click="getDataList()" @click="(e)=>{this.queryShow=e}" @searchReset="searchReset()">
      <el-col :span="6">
        <el-form-item label="表单编码">
          <el-input v-model="dataForm.formCode" placeholder="表单编码" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="表单名称">
          <el-input v-model="dataForm.formName" placeholder="表单名称" clearable></el-input>
        </el-form-item>
      </el-col>
       <el-col :span="6">
        <el-form-item label="建模编码">
          <el-input v-model="dataForm.modelCode" placeholder="建模编码" clearable></el-input>
        </el-form-item>
      </el-col>
      <template v-slot:other>
        <el-col :span="6">
          <el-form-item label="状态">
            <el-select v-model="dataForm.status" placeholder="表单状态" clearable>
              <el-option v-for="template in $getDictList('SYS_STATUS', true)" :key="template.val"
                :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </template>
  </uni-list-query-card>
    <el-card shadow="never" class="aui-card-list-body">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="$hasPermission('drs:drsFormDef:save')" type="primary" @click="addOrUpdateHandle()">
            {{ $t('add') }}</el-button>
          <el-button v-if="$hasPermission('drs:drsFormDef:delete')" type="danger" @click="deleteHandle()">
            {{ $t('deleteBatch') }}</el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getDataList()" circle></el-button>
        </template>
      </vxe-toolbar>
      <div class="aui-card-list-body-table">
      <vxe-table ref="xTable" :loading="dataListLoading" class="vxe-table-element" auto-resize
        @select-change="dataListSelectionChangeHandleVex" @select-all="dataListSelectionAllChangeHandleVex"
        :data="dataList" max-height="auto">
        <vxe-table-column type="checkbox" align="center" width="60"></vxe-table-column>
        <vxe-table-column field="formCode" title="表单编码">
        </vxe-table-column>
        <vxe-table-column field="formName" title="表单名称">
        </vxe-table-column>
        <vxe-table-column field="formClass" title="表单分类">
          <template slot-scope="scope">
            {{$getDictLabel('FORM_CLASS', scope.row.status)}}
          </template>
        </vxe-table-column>
        <vxe-table-column field="modelCode" title="数据库表名">
        </vxe-table-column>
        <vxe-table-column field="updateDate" title="更新时间">
        </vxe-table-column>
        <vxe-table-column title="handle" width="300" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('drs:drsFormDef:update')" type="text" size="small" icon="el-icon-edit"
              @click="addOrUpdateHandle(scope.row)">{{ $t('update') }}</el-button>
            <el-button type="text" size="small" icon="el-icon-coin" @click="tableDesign(scope.row)">数据建模</el-button>
            <el-button v-if="scope.row.formType === '0'" type="text" size="small" icon="el-icon-s-grid"
              @click="formDesign(scope.row)">设计表单</el-button>
            <el-button v-if="$hasPermission('drs:drsFormDef:delete')" type="text" size="small" icon="el-icon-delete"
              @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </vxe-table-column>
      </vxe-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <table-design v-if="tableDesignVisible" ref="tableDesign" @openTableDesign="openTableDesign"></table-design>
      </div>
    </el-card>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './drsFormDef-add-or-update'
import TableDesign from './drsFormDef-tableDesign'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      searchShow: true,
      tableDesignVisible: false,
      mixinViewModuleOptions: {
        getDataListURL: '/drs/drsFormDef/page',
        getDataListIsPage: true,
        deleteURL: '/drs/drsFormDef',
        deleteIsBatch: true,
        exportURL: '/drs/drsFormDef/export'
      },
      dataForm: {
        formCode: '',
        formName: '',
        modelCode: '',
        status: ''
      }
    }
  },
  components: {
    AddOrUpdate,
    TableDesign
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (row) {
      let id
      if (row) {
        id = row.id
      }
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.init()
      })
    },
    tableDesign (row) {
      if (!row.modelFlag) {
        this.tableDesignSel(row)
      } else {
        this.tableDesignEdit(row)
      }
    },
    tableDesignEdit (row) {
      // 打开路由
      var router = {
        name: 'dpd-dpdTable-editForm',
        id: '',
        menuId: this.$route.meta.menuId,
        oldName: 'drs-drsFormDef',
        path: 'dpd/dpdTable-editForm',
        title: '编辑数据库模型--' + row.formName
      }
      var params = {
        timestamp: this.$xeutils.timestamp(),
        id: row.modelId,
        formFlag: true,
        formId: row.id
      }
      this.$options.methods.openNewTab(this.$router, router, params)
    },
    tableDesignSel (row) {
      this.tableDesignVisible = true
      this.$nextTick(() => {
        this.$refs.tableDesign.dataForm.id = row.id
        this.$refs.tableDesign.init()
      })
    },
    openTableDesign (flag, row) {
      if (flag) {
        this.tableDesignEdit(row)
      } else {
        this.getDataList()
      }
    },
    formDesign (row) {
      if (row.modelFlag) {
        this.designUrl = `/#/drsFormDesign?id=${row.id}`
        window.open(this.designUrl, '_blank')
      } else {
        this.$message.error('请先数据建模')
      }
    }
  }
}
</script>
